{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.7375)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_tensor = torch.FloatTensor([1,0,1,0])\n",
    "out_tensor = torch.FloatTensor([0.6, 0.6, 0.6, 0.6])\n",
    "loss = nn.BCEWithLogitsLoss()\n",
    "loss(out_tensor, target_tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.4375, 1.0375, 0.4375, 1.0375])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_tensor = torch.FloatTensor([1,0,1,0])\n",
    "out_tensor = torch.FloatTensor([0.6, 0.6, 0.6, 0.6])\n",
    "loss = nn.BCEWithLogitsLoss(reduction='none')\n",
    "loss(out_tensor, target_tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.7375)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def _reduce_loss(loss):\n",
    "    return loss.sum() / loss.shape[0]\n",
    "\n",
    "target_tensor = torch.FloatTensor([1,0,1,0])\n",
    "out_tensor = torch.FloatTensor([0.6, 0.6, 0.6, 0.6])\n",
    "loss = nn.BCEWithLogitsLoss(reduction='none')\n",
    "_reduce_loss(loss(out_tensor, target_tensor))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
